﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using thousandclear.Data;
using thousandClear.Areas.Admin.Models;
using thousandClear.Util.Media;
using thousandClear.Util.Setting;

namespace thousandClear.Areas.Admin.Controllers
{
    public class AuntController : BaseController
    {
        // GET: Admin/Aunt
        public ActionResult Index(int? page, int? count)
        {
            string level = "1";
            level = GetCookie("level");
            ViewBag.level = level;
            page = page ?? 1;
            count = count ?? 10;
            int totalCount = 0;
            int totalPage = 1;
            int currentPage = page.Value;
            int pageCount = count.Value;
            List<AuntModel> list = new List<AuntModel>();
            using (var db = new qianjingEntities())
            {
                var query = db.O_Aunt.AsQueryable();
                var records = query.OrderByDescending(a => a.OATID).Where(a => a.RowStatus != 2).Skip((page.Value - 1) * count.Value).Take(count.Value).ToList();
                totalCount = query.Count();
                foreach (var item in records)
                {
                    list.Add(new AuntModel
                    {
                        OATID = Convert.ToInt16(item.OATID),
                        oatName = item.oatName,
                        oatPhone = item.oatPhone,
                        oatDesc = item.oatDesc,
                        CreatedDate = item.CreatedDate,
                        RowStatus = item.RowStatus,
                        oatNo = item.oatNo,
                        IDNo=item.IDNo,
                        TotalScore=item.oatTotalScore
                    });
                }

            }


            ViewBag.TotalCount = totalCount;
            totalPage = int.Parse((totalCount / count).ToString()) + (totalCount % count == 0 ? 0 : 1);
            ViewBag.TotalPage = totalPage;
            ViewBag.CurrentPage = currentPage;
            ViewBag.PageCount = pageCount;

            return View(list);
        }
        //HttpGet: Admin/Aunt
        [HttpGet]
        public AuntControllerResponse remove(long id)
        {
            AuntControllerResponse resp = new AuntControllerResponse();

            using (var db = new qianjingEntities())
            {

                var query = db.O_Aunt.FirstOrDefault(a => a.OATID == id);

                if (query == null)
                {
                    resp.ErrMsg = "该记录不存在";
                    return resp;
                }
                query.RowStatus = 2;

                db.SaveChanges();
            }
            return resp;


        }
        // GET: Aunt/Add
        public ActionResult Add()
        {
            return View();
        }
        // POST: Aunt/Add
        [HttpPost]
        public ActionResult Add(string data)
        {

            AppSetting setting = new AppSetting();
            ImageResolver resolver = new ImageResolver();
            var post = JObject.Parse(data);

            string AuntName = post["AuntName"].Value<string>();
            string AuntDesc = post["AuntDesc"].Value<string>();
            string AuntPhone = post["AuntPhone"].Value<string>();
            string oatNo = post["oatNo"].Value<string>();
            string imgUrl = post["imgUrl"].Value<string>();
            string strHealthImg = post["HealthImg"].Value<string>();
            string strIDNo = post["IDNo"].Value<string>();
            string filePath = setting.AppUploadFolder;
            string imgFileName = string.Empty;
            string imgHealthName = string.Empty;
            //保存头像
            if (!string.IsNullOrEmpty(imgUrl))
            {
                imgFileName = Guid.NewGuid().ToString() + "." + resolver.GetExt(imgUrl);
                resolver.SaveBase64(imgUrl, filePath, imgFileName);
            }
            //保存健康证
            if (!string.IsNullOrEmpty(strHealthImg))
            {
                imgHealthName = Guid.NewGuid().ToString() + "." + resolver.GetExt(strHealthImg);
                resolver.SaveBase64(strHealthImg, filePath, imgHealthName);
            }
            using (var db = new qianjingEntities())
            {

                db.O_Aunt.Add(new O_Aunt
                {
                    oatPhone = AuntPhone,
                    oatName = AuntName,
                    oatDesc = AuntDesc,
                    RowStatus = 1,
                    oatImg = imgFileName,
                    CreatedDate = DateTime.Now,
                    oatNo = oatNo,
                    oatHealthImg= imgHealthName,
                    IDNo=strIDNo
                });
                db.SaveChanges();
            }

            return RedirectToAction("Add");

        }
        // GET: Aunt/Edit
        [HttpPost]
        public ActionResult Edit(string data)
        {
            AppSetting setting = new AppSetting();
            ImageResolver resolver = new ImageResolver();
            var post = JObject.Parse(data);
            int OATID = post["OATID"].Value<int>();
            string oatName = post["oatName"].Value<string>();
            string oatPhone = post["oatPhone"].Value<string>();
            string oatDesc = post["oatDesc"].Value<string>();
            string oatNo = post["oatNo"].Value<string>();
            string imgUrl = post["imgUrl"].Value<string>();
            string strHealthImg = post["HealthImg"].Value<string>();
            string strIDNo = post["IDNo"].Value<string>();
            string filePath = setting.AppUploadFolder;
            string imgFileName = string.Empty;
            string imgHealthName = string.Empty;
            using (var db = new qianjingEntities())
            {
                var query = db.O_Aunt.FirstOrDefault(a => a.OATID == OATID);
                //修改头像
                if (!string.IsNullOrEmpty(imgUrl) && query.oatImg != imgUrl)
                {
                    imgFileName = Guid.NewGuid().ToString() + "." + resolver.GetExt(imgUrl);
                    resolver.SaveBase64(imgUrl, filePath, imgFileName);

                    query.oatImg = imgFileName;
                }
                //修改健康证
                if (!string.IsNullOrEmpty(strHealthImg) && query.oatHealthImg != strHealthImg)
                {
                    imgHealthName = Guid.NewGuid().ToString() + "." + resolver.GetExt(strHealthImg);
                    resolver.SaveBase64(strHealthImg, filePath, imgHealthName);

                    query.oatHealthImg = imgHealthName;
                }

                query.oatName = oatName;
                query.oatPhone = oatPhone;
                query.oatDesc = oatDesc;
                query.oatNo = oatNo;
                query.UpdatedDate = DateTime.Now;
                //query.oatHealthImg = strHealthImg;
                query.IDNo = strIDNo;
                db.SaveChanges();
            }

            return RedirectToAction("Index");

        }
        // GET: Aunt/Edit
        public ActionResult Edit(int id)
        {
            AppSetting setting = new AppSetting();
            AuntModel model = new AuntModel();
            using (var db = new qianjingEntities())
            {
                var query = db.O_Aunt.FirstOrDefault(a => a.OATID == id);
                if (query != null)
                {
                    model.OATID = Convert.ToInt16(query.OATID);
                    model.oatName = query.oatName;
                    model.oatPhone = query.oatPhone;
                    model.oatDesc = query.oatDesc;
                    model.oatNo = query.oatNo;
                    model.ImgUrl = query.oatImg;
                    model.UploadUrl = "http://www.qianjing-china.com/thousandClear/Statics/Upload/";
                    model.HealthImg = query.oatHealthImg;
                    model.IDNo = query.IDNo;
                    model.TotalScore = query.oatTotalScore;
                }
            }
            return View(model);
        }
        // GET: Aunt/LogDetail
        public ActionResult LogDetail(int? page, int? count, string id,string status)
        {
            page = page ?? 1;
            count = count ?? 10;
            int totalCount = 0;
            int totalPage = 1;
            int currentPage = page.Value;
            int pageCount = count.Value;
            AppSetting setting = new AppSetting();

            List<SP_orderlog_info2_Result> list = new List<SP_orderlog_info2_Result>();
            using (var db = new qianjingEntities())
            {

                var query = db.SP_orderlog_info2(id).OrderByDescending(a => a.CreatedDate).ToList();
                //if (!string.IsNullOrEmpty(status)) {
                //    query = query.Where(a => a.RowStatus == int.Parse(status)).ToList();
                //}
                totalCount = query.Count();

                var records = query.Skip((page.Value - 1) * count.Value).Take(count.Value).ToList();
                if (records != null)
                {
                    foreach (var item in records)
                    {
                        list.Add(new SP_orderlog_info2_Result
                        {
                            //CreatedDate = item.CreatedDate,
                            //dimDesc = item.dimDesc,
                            //oasName = item.oasName,
                            //tcGrades1 = item.tcGrades1,
                            //tcGrades2 = item.tcGrades2,
                            //tcGrades3 = item.tcGrades3,
                            //tcGrades4 = item.tcGrades4,
                            //tcGrades5 = item.tcGrades5,
                            //tcGrades6 = item.tcGrades6,
                            //tcGrades7 = item.tcGrades7,
                            //tcGrades8 = item.tcGrades8,
                            //toSerialNo = item.toSerialNo,
                            //tcRemark=item.tcRemark
                            CreatedDate = item.CreatedDate,
                            tcGrades1 = item.tcGrades1,
                            tcGrades2 = item.tcGrades2,
                            tcGrades3 = item.tcGrades3,
                            tcGrades4 = item.tcGrades4,
                            tcGrades5 = item.tcGrades5,
                            tcGrades6 = item.tcGrades6,
                            tcGrades7 = item.tcGrades7,
                            tcGrades8 = item.tcGrades8,
                            ocNicName = item.ocNicName,
                            oatName=item.oatName,
                            CreatedBy=item.CreatedBy,
                            tcRemark = item.tcRemark

                        });

                    }
                }

            }
            ViewBag.TotalCount = totalCount;
            totalPage = int.Parse((totalCount / count).ToString()) + (totalCount % count == 0 ? 0 : 1);
            ViewBag.TotalPage = totalPage;
            ViewBag.CurrentPage = currentPage;
            ViewBag.PageCount = pageCount;
            ViewBag.id = id;
            return View(list);
        }
        // GET: Aunt/LogDetail
        public ActionResult GradesDetail(int? page, int? count, string id, string status,string name)
        {
            page = page ?? 1;
            count = count ?? 10;
            int totalCount = 0;
            int totalPage = 1;
            int currentPage = page.Value;
            int pageCount = count.Value;
            AppSetting setting = new AppSetting();

            List<T_Reward> list = new List<T_Reward>();
            using (var db = new qianjingEntities())
            {

                var query = db.T_Reward.OrderByDescending(a => a.CreatedDate).ToList();
                if (!string.IsNullOrEmpty(status))
                {
                    query = query.Where(a => a.RowStatus == int.Parse(status)).ToList();
                }
                if (!string.IsNullOrEmpty(name))
                {
                    var Customer = db.O_Customer.FirstOrDefault(a => a.ocNicName == name);
                    query = query.Where(a => a.trOCID == Customer.OCID).ToList();
                }
                totalCount = query.Count();

                var records = query.Where(a=>a.trOAID.ToString()==id).Skip((page.Value - 1) * count.Value).Take(count.Value).ToList();
                if (records != null)
                {
                    foreach (var item in records)
                    {
                        var ocNicName = db.O_Customer.FirstOrDefault(a=>a.OCID==item.trOCID).ocNicName;
                        list.Add(new T_Reward
                        {
                            CreatedDate = item.CreatedDate,
                            RowStatus=item.RowStatus,
                            trTotolRate=item.trTotolRate,
                            UpdatedBy=ocNicName,
                        });

                    }
                }

            }
            ViewBag.TotalCount = totalCount;
            totalPage = int.Parse((totalCount / count).ToString()) + (totalCount % count == 0 ? 0 : 1);
            ViewBag.TotalPage = totalPage;
            ViewBag.CurrentPage = currentPage;
            ViewBag.PageCount = pageCount;
            ViewBag.id = id;
            return View(list);
        }
        //HttpGet: Admin/LogDetail
        [HttpGet]
        public int SettlementGrade(long id)
        {
            int i = 1;

            using (var db = new qianjingEntities())
            {

                var query = db.T_Reward.FirstOrDefault(a => a.TRID == id);

                if (query == null)
                {
                    i = 2;
                    return i;
                }
                query.RowStatus = 2;
                query.UpdatedDate=DateTime.Now;       
                db.SaveChanges();
            }
            return 1;


        }


        // GET: Aunt/LogDetail
        public ActionResult ClearLog(int? page, int? count, string id, string status)
        {
            page = page ?? 1;
            count = count ?? 10;
            int totalCount = 0;
            int totalPage = 1;
            int currentPage = page.Value;
            int pageCount = count.Value;
            AppSetting setting = new AppSetting();

            List<SP_clearLog_info_Result> list = new List<SP_clearLog_info_Result>();
            using (var db = new qianjingEntities())
            {

                var query = db.SP_clearLog_info(id).OrderByDescending(a => a.CreatedDate).ToList();
                //if (!string.IsNullOrEmpty(status)) {
                //    query = query.Where(a => a.RowStatus == int.Parse(status)).ToList();
                //}
                totalCount = query.Count();

                var records = query.Skip((page.Value - 1) * count.Value).Take(count.Value).ToList();
                if (records != null)
                {
                    foreach (var item in records)
                    {
                        list.Add(new SP_clearLog_info_Result
                        {
                            CreatedDate=item.CreatedDate,
                            oasNo= string.IsNullOrEmpty(item.oasNo) ? "N/A" : item.oasNo,
                            oasName= string.IsNullOrEmpty(item.oasName) ? "N/A" : item.oasName,
                            oasAddressDetail = string.IsNullOrEmpty(item.oasAddressDetail) ? "N/A" : item.oasAddressDetail
                        });

                    }
                }

            }
            ViewBag.TotalCount = totalCount;
            totalPage = int.Parse((totalCount / count).ToString()) + (totalCount % count == 0 ? 0 : 1);
            ViewBag.TotalPage = totalPage;
            ViewBag.CurrentPage = currentPage;
            ViewBag.PageCount = pageCount;
            ViewBag.id = id;
            return View(list);
        }
    }
}